import router from './index';
import store from '../store';
import appName from '../utils/environments';

router.beforeEach((to, from, next) => {
  // if (to && to.meta && to.meta.title) {
  //   // document.title = to.meta.title;
  // }
  const app = store.state.app;
  // console.log(app.trade);
  if (to && (to.path === '/embed-trade' || to.path === '/embed-trade/')) {
    const symbols = to.query.symbols || 1;
    if (app && app.trade && app.trade.symbol) {
      router.push({ path: `/embed-trade/${app.trade.symbol}?symbols=${symbols}&ref=${to.query.ref}` })
    } else {
      router.push({ path: `/embed-trade/newdexissuer-ndx-eos?symbols=${symbols}&ref=${to.query.ref}` })
    }
  }
  if (to && (to.path === '/trade' || to.path === '/trade/')) {
    let appSymbol = app && app.trade ? app.trade.symbol : '';
    let defalutSymbol = 'newdexissuer-ndx-eos';
    if (appName === 'tronNewdex') {
      appSymbol = appSymbol ? appSymbol.toUpperCase() : '';
      defalutSymbol = 'TRX_USDT';
    }
    if (app && app.trade && app.trade.symbol) {
      router.push({ path: `/trade/${appSymbol}` })
    } else {
      router.push({ path: `/trade/${defalutSymbol}` })
    }
  }
  document.documentElement.scrollTop = 0;
  next();
  // console.log(`路由请求前: ${to}, ${from}, ${next}, ${to.meta.title}`);
});

// console.log(`路由请求后: ${to}, ${from}, ${next}`);
router.afterEach((to) => {
  if (to.name && to.name !== 'nopage') {
    sessionStorage.setItem('afterRouteName', to.name);
  }
  // 退出登录
  const scat = store.state.app.scatter;
  if (scat && !scat.identity && to && to.meta && to.meta.isUser) {
    router.push('/');
  }
});
